package com.zhilianbao.leyaogo.service;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.bql.sharesdk.utils.ShareActionListener;
import com.bql.sharesdk.utils.ShareUtil;
import com.bql.utils.EventManager;
import com.yixia.camera.util.Log;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.ui.activity.QuickLoginActivity;
import com.zhilianbao.leyaogo.ui.activity.goodscategory.GoodsActivity;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zlb.leyaoxiu2.live.LegoJumpCallback;
import com.zlb.leyaoxiu2.live.LegoShareCallback;
import com.zlb.leyaoxiu2.live.ShareCallback;
import com.zlb.leyaoxiu2.live.common.utils.StringUtils;
import com.zlb.lxlibrary.service.LeXiuInitService;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;

/**
 * ClassName: LeYaoGoService <br>
 * Description: 乐摇秀服务类 与乐秀建立关联<br>
 * Author: Cyarie <br>
 * Created: 2016/12/20 10:56 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class LeYaoGoService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("LIVE", "LegoService onCreate");
        Log.setLog(true);
        ShareUtil.initSDK(this);
        LeXiuInitService.getInstance().initService(this);

        //实例化界面跳转
        LeXiuInitService.getInstance().initLegoJumpCallback(new LegoJumpCallback() {

            @Override
            public void finishAllNoMainActivity() {
                //发送通知 关闭相关Activity
                EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_LEXIU_CLOSE_VIDEO));
            }

            @Override
            public void jumpToLoginActivity(Context context) {
                //跳转登录界面
                //                Intent intent = new Intent(context, LoginActivity.class);
                //                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                //                context.startActivity(intent);
                if (context instanceof Activity) {
                    Intent intent = new Intent(context, QuickLoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                    context.startActivity(intent);
                } else {
                    EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_GO_TO_LOGIN_BY_LEXIU));
                    Utils.gotoLogin();
                }

            }

            @Override
            public void jumpToGoodsActivity(Context context, String goodsId) {
                //根据商品Id跳转商品界面
                //Utils.startGoodsDetails((Activity) context, Integer.valueOf(goodsId), 0, false);
                if (goodsId != null && StringUtils.isNumber(goodsId)) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(IntentConfig.BUNDLE_GOODS_ID, Integer.valueOf(goodsId));
                    bundle.putInt(IntentConfig.BUNDLE_GOODS_SKU_ID, 0);
                    bundle.putBoolean(IntentConfig.IS_FROM_SHOPPING_CART, false);
                    Intent intent = new Intent(context, GoodsActivity.class);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            }
        });

        LeXiuInitService.getInstance().initLegoShareCallback(new LegoShareCallback() {
            @Override
            public void onShareWechatFriend(Context context, String title, String content, String jumpUrl, String imageUrl, ShareCallback shareCallback) {
                ShareUtil.shareWxFriend(context, title, content, imageUrl, jumpUrl, getPlatformActionListener(shareCallback));
            }

            @Override
            public void onShareWechatLine(Context context, String title, String content, String jumpUrl, String imageUrl, ShareCallback shareCallback) {
                //分享朋友圈
                ShareUtil.shareWxMoment(context, title, content, imageUrl, jumpUrl, getPlatformActionListener(shareCallback));
            }

            @Override
            public void onShareSinaWeibo(Context context, String content, String imageUrls, ShareCallback shareCallback) {
                //分享新浪微博
                ShareUtil.shareSinaWeibo(context, content, imageUrls, getPlatformActionListener(shareCallback));
            }

            @Override
            public void onShareQQ(Context context, String title, String content, String jumpUrl, String imageUrl, ShareCallback shareCallback) {
                //分享QQ
                ShareUtil.shareQqFriend(context, title, jumpUrl, content, imageUrl, getPlatformActionListener(shareCallback));
            }

            @Override
            public void onShareQZONE(Context context, String title, String content, String jumpUrl, String imageUrl, ShareCallback shareCallback) {
                //分享QQ空间
                ShareUtil.shareQzone(context, title, jumpUrl, content, imageUrl, title, jumpUrl, content, getPlatformActionListener(shareCallback));
            }
        });
    }

    public ShareActionListener getPlatformActionListener(ShareCallback shareCallback) {
        if (shareCallback != null) {
            return new ShareActionListener() {
                @Override
                public void onClientInvalid() {
                    shareCallback.onShareEventCallback(-2);
                }

                @Override
                public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                    shareCallback.onShareEventCallback(0);
                }

                @Override
                public void onError(Platform platform, int i, Throwable throwable) {
                    shareCallback.onShareEventCallback(-1);
                }

                @Override
                public void onCancel(Platform platform, int i) {
                    shareCallback.onShareEventCallback(1);
                }
            };
        } else {
            return null;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("LIVE", "LegoService onstartCommand");
        LeXiuInitService.getInstance().onServiceStartCommand();
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("LIVE", "LegoService onDestroy");
        LeXiuInitService.getInstance().onDestroySerivce();
    }
}
